<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>webscoket客户端</title>
</head>
<body>


    <form onsubmit="return false;">

        <input type="text" name="message" value="Netty 实践">
        <br/><br/>
        <input type="button" value="发送 websocket 请求消息"
               onclick="send(this.form.message.value)"/>
        <hr color="blue">
        <h3>服务端返回应答消息</h3>
        <textarea id="responseText" style="width: 500px;height:300px"></textarea>
    </form>
    <script type="text/javascript">

        var socket;
        var ta = document.getElementById('responseText');
        if(!window.WebSocket){
            window.WebSocket = window.MozWebSocket;
        }
        if(window.WebSocket){
            socket = new WebSocket("ws://localhost:8080/websocket");
            //注册事件
            socket.onmessage=function(event){
                ta.value="";
                ta.value = event.data;
            }
            socket.onopen = function(event){
                ta.value="";
                ta.value='打开 WebSocket 服务正常，浏览器支持WebSocket！';
            }

            socket.onclose=function(event){
                ta.value="";
                ta.value="WebSocket 关闭"
            }
        }else{
            alert("抱歉，您的浏览器不支持WebSocket！");
        }

        function send(message){
            if(!window.WebSocket){return;}
            if(socket.readyState == WebSocket.OPEN){
                socket.send(message);
            }else{
                alert("WebSocket连接没有建立成功、")
            }
        }

    </script>
</body>
</html>